package com.zj.wfsdk;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;

public class WifiCommunication
{
    private final String TAG = "WifiCommunication";
    private static Socket client = null;
    private static OutputStream out = null;
    private static BufferedReader in = null;
    private final Handler mHandler;
    private String AddressIp = null;
    private int port = 0;
    private WifiCommunication.ConnectThread mConnection = null;
    public static final int WFPRINTER_CONNECTED = 0;
    public static final int WFPRINTER_DISCONNECTED = 1;
    public static final int WFPRINTER_CONNECTEDERR = 2;
    public static final int SEND_FAILED = 4;
    public static final int CONNECTION_LOST = 5;


    public WifiCommunication(Handler handler)
    {
        this.mHandler = handler;
    }

    public void initSocket(String AddressIp, int port)
    {
        this.AddressIp = AddressIp;
        this.port = port;
        if (this.mConnection != null)
        {
            this.mConnection = null;
        }

        if (this.mConnection == null)
        {
            //this.mConnection = new WifiCommunication.ConnectThread(null);
            this.mConnection = new WifiCommunication.ConnectThread();
            this.mConnection.start();
        }

    }

    public void sendMsg(String sndMsg, String charset)
    {
        Message msg;
        try
        {
            byte[] e;
            try
            {
                e = sndMsg.getBytes(charset);
            }
            catch (UnsupportedEncodingException var5)
            {
                e = sndMsg.getBytes();
            }

            if (client.isConnected() && !client.isOutputShutdown())
            {
                Log.d(TAG, "[sendMsg] data sending...");
                out.write(e);
                out.flush();
                Log.d(TAG, "[sendMsg] done");
            }
            else
                Log.d(TAG, "[sendMsg] client disconnected or output shutdown");
        }
        catch (Exception ex)
        {
            Log.d(TAG, "[sendMsg] " + ex.toString());
            msg = this.mHandler.obtainMessage(SEND_FAILED);
            this.mHandler.sendMessage(msg);
        }

    }

    public void sndByte(byte[] send)
    {
        Message msg_ret;
        try
        {
            if (client.isConnected() && !client.isOutputShutdown())
            {
                Log.d(TAG, "[sndByte] data sending...");
                out.write(send);
                out.flush();
                Log.d(TAG, "[sndByte] done");
            }
            else
                Log.d(TAG, "[sndByte] client disconnected or output shutdown");
        }
        catch (Exception ex)
        {
            Log.d(TAG, "[sndByte] " + ex.toString());
            msg_ret = this.mHandler.obtainMessage(SEND_FAILED);
            this.mHandler.sendMessage(msg_ret);
        }

    }

    public void close()
    {
        try
        {
            if (out != null)
            {
                out.close();
            }

            if (in != null)
            {
                in.close();
            }

            if (client != null)
            {
                client.close();
                out = null;
                in = null;
                client = null;
                Message e = this.mHandler.obtainMessage(WFPRINTER_DISCONNECTED);
                this.mHandler.sendMessage(e);
            }
        }
        catch (Exception ex)
        {
            Log.d(TAG, ex.toString());
        }

    }

    private class ConnectThread extends Thread
    {

        private ConnectThread()
        {
        }

        public void run()
        {
            Message msg_ret;
            try
            {
				Log.d(TAG, "[ConnectThread] Init socket...");
                InetAddress e = InetAddress.getByName(WifiCommunication.this.AddressIp);
                WifiCommunication.client = new Socket(e, WifiCommunication.this.port);
                if (WifiCommunication.client != null)
                {
					Log.d(TAG, "[ConnectThread] Get output stream...");
                    WifiCommunication.out = WifiCommunication.client.getOutputStream();
					Log.d(TAG, "[ConnectThread] Get input stream...");
                    WifiCommunication.in = new BufferedReader(
                            new InputStreamReader(WifiCommunication.client.getInputStream())
                    );
                }

                if (WifiCommunication.client != null
                        && WifiCommunication.out != null && WifiCommunication.in != null)
                {
					Log.d(TAG, "[ConnectThread] Notify event WFPRINTER_CONNECTED");
                    msg_ret = WifiCommunication.this.mHandler.obtainMessage(WFPRINTER_CONNECTED);
                    WifiCommunication.this.mHandler.sendMessage(msg_ret);
                }
            }
            catch (Exception ex)
            {
                Log.d(TAG, "[ConnectThread] " + ex.toString());

                msg_ret = WifiCommunication.this.mHandler.obtainMessage(WFPRINTER_CONNECTEDERR);
                WifiCommunication.this.mHandler.sendMessage(msg_ret);
            }
            Log.d(TAG, "[ConnectThread] DONE");
        }

        // $FF: synthetic method
        /*ConnectThread(WifiCommunication.ConnectThread var2)
        {
            this();
        }*/
    }
}
